package hust.cs.javacourse.search.run;

import hust.cs.javacourse.search.index.AbstractDocumentBuilder;
import hust.cs.javacourse.search.index.AbstractIndex;
import hust.cs.javacourse.search.index.AbstractIndexBuilder;
import hust.cs.javacourse.search.index.impl.DocumentBuilder;
import hust.cs.javacourse.search.index.impl.Index;
import hust.cs.javacourse.search.index.impl.IndexBuilder;
import hust.cs.javacourse.search.util.Config;

import java.io.File;

/**
 * 测试索引构建
 */
public class TestBuildIndex {
    /**
     *  索引构建程序入口
     * @param args : 命令行参数
     */
    public static void main(String[] args){
        AbstractDocumentBuilder documentBuilder = new DocumentBuilder();
        AbstractIndexBuilder indexBuilder = new IndexBuilder(documentBuilder);
        String rootDir = Config.DOC_DIR;

        //开始构建索引目录
        AbstractIndex index = indexBuilder.buildIndex(rootDir);
        System.out.println("\n开始构建索引……");
        System.out.println("索引构建一览：");
        index.docIdToDocPathMapping.forEach((key,val)->{
            System.out.println("Path of Document" + key + ": " + val);
        });
        System.out.print("\n");
        index.termToPostingListMapping.forEach((key,val)->{
            System.out.println(key + ":\n" + val + "\n");
        });

        //将索引保存到文件
        String indexFile = Config.INDEX_DIR + "index.dat";
        index.save(new File(indexFile));
        //从文件中读取
        AbstractIndex index0 = new Index(); //创建一个空的 index
        index0.load(new File(indexFile)); //从文件加载对象的内容
        System.out.println("Loading index from index.dat...\n");
        index.docIdToDocPathMapping.forEach((key,val)->{
            System.out.println("Path of Document" + key + ": " + val);
        });
        System.out.print("\n");
        index.termToPostingListMapping.forEach((key,val)->{
            System.out.println(key + ":\n" + val + "\n");
        });
    }
}
